package com.goods.controller.business;

import com.github.pagehelper.PageInfo;
import com.goods.business.service.ProductService;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * projectName: goods
 *
 * @author: ysc
 * time: 2022/12/3 21:52 周六
 * description:
 */

@Api(tags = "业务管理-物资资料")
@RestController
@RequestMapping("/business/product")
public class ProductController {

    @Autowired
    private ProductService productService;



    //物资资料列表
    @ApiOperation("物资资料列表")
    @GetMapping("/findProductList")
    public ResponseBean findProductList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize") Integer pageSize,
                                        @RequestParam(value = "categorys", required = false) String categorys,
                                        ProductVO productVO ){
        buildCategorySearch(categorys,productVO);
        PageVO<ProductVO> productVOPageVO=productService.findProductList(pageNum,pageSize,productVO);
        return ResponseBean.success(productVOPageVO);
    }

    private void buildCategorySearch(String categorys, ProductVO productVO) {
        if (categorys != null && !"".equals(categorys)){
            String[] split = categorys.split(",");
            switch (split.length){
                case 1:
                    productVO.setOneCategoryId(Long.parseLong(split[0]));
                    break;
                case 2:
                    productVO.setOneCategoryId(Long.parseLong(split[0]));
                    productVO.setTwoCategoryId(Long.parseLong(split[1]));
                    break;
                case 3:
                    productVO.setOneCategoryId(Long.parseLong(split[0]));
                    productVO.setTwoCategoryId(Long.parseLong(split[1]));
                    productVO.setThreeCategoryId(Long.parseLong(split[3]));
                    break;
            }
        }
    }


    //分类列表
    @GetMapping("categoryTree")
    private ResponseBean categoryTree(){
        productService.categoryTree();
        return ResponseBean.success();
    }


    //添加
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductVO productVO){
        productService.add(productVO);
        return ResponseBean.success();
    }

    //移入回收站
    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id) throws SystemException {
        productService.remove(id);
        return ResponseBean.success();
    }


    //拿出回收站
    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id){
        productService.back(id);
        return ResponseBean.success();
    }

    //通过验证
    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        productService.publish(id);
        return  ResponseBean.success();
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        productService.delete(id);
        return  ResponseBean.success();
    }


    //库存列表（左侧图使用）
    @ApiOperation(value = "库存列表（左侧图使用）")
    @GetMapping("/findAllStocks")
    public ResponseBean findAllStocks(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "9") Integer pageSize){
        List<ProductStockVO> productList = productService.findAllStocks(pageNum,pageSize);
        return ResponseBean.success(productList);
    }

    //库存列表（右侧图使用）
    @ApiOperation(value = "库存列表（右侧图使用）")
    @GetMapping("/findProductStocks")
    public ResponseBean findProductStocks(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "9") Integer pageSize){
        PageVO<ProductStockVO> productStocks =productService.findProductStocks(pageNum,pageSize);
        return ResponseBean.success(productStocks);
    }

}
